<--- %%NOBANNER%% --> sample.sas
 BackForward

/*------------------<--  Start of Description -->--------------------\
| Samples with Replacement;                                          |
|--------------------<--  End of Description -->---------------------|
|--------------------------------------------------------------------|
|--------------<--  Start of Files or Arguments Needed -->-----------|
| Arguments:                                                         |
|   Indata=name of input dataset                                     |
|   var=variable on which the bootstrap samples are to be drawn      |
|       one or more variables, variables must be separated by space, |
|       all variables given will be left in the output dataset;      |
|   bootn= B, number of samples you want to generate;                |
|   Seed= initial seed for RANUNI function                           |
|   outseed=output seed name, usually when bootstrap needs more than |
|           one loop, we may need to run the same function repeatedly|
|           so we may need to keep the seed generated previously from|
|           this function;                                           |
|   Outdata=name of the output dataset containing the bootstrap      |
|           samples.  There is one observation for each sample with  |
|           the n members of the sample contained in the variables   |
|           BS&x.1-BS&x.N .                                          |
|---------------<--  End of Files or Arguments Needed -->------------|
|--------------------------------------------------------------------|
|----------------<--  Start of Example and Usage -->-----------------|
| Example:                                                           |
|  data case;                                                        |
|     retain Seed_1 45;                                              |
|     do i=1 to 10;                                                  |
|        call ranuni(Seed_1,X1);                                     |
|        x1=ceil(x1*10);                                             |
|        n=i;                                                        |
|        output;                                                     |
|     end;                                                           |
|  run;                                                              |
|  %prt(case);                                                       |
|  %sample(Indata=case,var=x1 n1,seed=679897321,outdata=two,         |
|          outseed=myseed);                                          |
|  %prt(two);                                                        |
|  %sample(Indata=case,var=x1,seed=&myseed,outdata=three,            |
|          outseed=myseed);                                          |
|  %prt(three);                                                      |
| Usage: sample(Indata=,var=,bootn=,seed=679897321,outdata=,outseed=)|
\-------------------<--  End of Example and Usage -->---------------*/
%MACRO sample(Indata=,var=,bootn=1,seed=679897321,outdata=, outseed=);
/*--------------------------------------------\
| Author:  E. Bergstralh;                     |
| Created: March 15, 1994;                    |
| Purpose: Select Bootstrap Samples;          |
\--------------------------------------------*/
%global &outseed resampleid; %local _Bootseed_; 
%if (%quote(%upcase(seed)) eq %quote(%upcase(&outseed))) %then %do;
   %put ==> Alert! The output seed cannot be "seed" or "SEED".;
   %goto finish;
%end;
%if (%chk_type(&seed) = 1) %then %let _Bootseed_=&seed;
%else %let _Bootseed_=679897321;
%if (%chk_type(&resampleid) ne 1) and (%chk_type(&bootn) ne 1) %then %let resampleid=1;
%else %if (%chk_type(&bootn) ne 1) %then %let resampleid=%eval(&resampleid+1);
%if (%quote(&outdata) ne) and (%quote(&indata) ne) and (%quote(&var) ne) %then %do;
   %if (%nobs(&indata) >= 1) %then %do;
   %let _bootvi_=0; 
   %do %while(%length(%nrbquote(%scan(%nrbquote(&var), %eval(&_bootvi_+1), %str((), )))));
      %let _bootvi_=%eval(&_bootvi_+1);
      %let _bootvari_=%nrbquote(%qscan(%nrbquote(&var), &_bootvi_, %str((), )));
      %if (%varnum(&indata, &_bootvari_) < 1) %then %do;
         %put ==> Alert! Input dataset %trim(%left(%upcase(&indata))) does not have variable or variables %trim(%left(%upcase(&_bootvari_)))!;
         %goto finish;
      %end;
   %end; %let _bootvi_=%eval(&_bootvi_+1);
   options nonotes;
   data &outdata;
      _Bootseed_=&_Bootseed_;
      %if (%chk_type(&bootn) = 1) %then %do;
      do booti=1 to &bootn;
      %end;
         do bootj=1 to _bootnobs_;
            _seed_=_Bootseed_;
            CALL RANUNI(_Bootseed_,_bootr_);
            resampleid=%if (%chk_type(&bootn) = 1) %then %do; booti %end; 
                       %else %do; &resampleid; %end;;
            _booti_=ceil(_bootr_*_bootnobs_);
            i=_booti_;
            set &indata(keep=&var) point=_booti_ nobs=_bootnobs_ ;
            output;
         end;
      %if (%chk_type(&bootn) = 1) %then %do;
      end;
      %end;
      CALL SYMPUT("_Bootseed_",_Bootseed_);
      stop;
      keep &var resampleid;
   RUN;
   options notes;
   %end;
   %else %put ==> Alert! Input dataset %trim(%left(%upcase(&indata))) does not exist or does not have any observations!;
%end;
%else %put ==> Alert! No output or input dataset!;
%if (%quote(&outseed) ne) %then %let &outseed=&_Bootseed_;
%finish:
%MEND sample;